﻿using System;

namespace TypeBuilderFx.Core
{
    /// <summary>
    /// Base class for the emitters of types acting as an adapter (wrapping an instance, an adaptee) supporting a base class
    /// and exposing one interface.
    /// </summary>
    public class SupportAdapteeTypeEmitterOptions : SupportBaseClassTypeEmitterOptions
    {
        private Type _adapteeType;

        /// <summary>The type being wrapped by the emitted type.</summary>
        public Type AdapteeType
        {
            get { return _adapteeType; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                _adapteeType = value;
            }
        }

        #region object Methods
        /// <summary>Must be overriden by derived classes in order to be able to use as a key in an dictionary.</summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode() ^ AdapteeType.GetHashCode();
        }

        /// <summary>Must be overriden by derived classes in order to be able to use as a key in an dictionary.</summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            SupportAdapteeTypeEmitterOptions options = obj as SupportAdapteeTypeEmitterOptions;

            return base.Equals(obj)
                && options != null
                && options.AdapteeType == AdapteeType;
        }
        #endregion

        /// <summary>Validates the adaptee type can't be <c>null</c>.</summary>
        public override void Validate()
        {
            base.Validate();

            if (AdapteeType == null)
            {
                throw new ArgumentNullException("AdapteeType");
            }
        }
    }
}